package com.hkts.naturalgas.curvesettings.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.curvesettings.entity.Curve;
import com.hkts.naturalgas.curvesettings.entity.CurveState;
import com.hkts.naturalgas.curvesettings.mapper.CurveStateMapper;
import com.hkts.naturalgas.curvesettings.service.ICurveStateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-10-12
 */
@Service
@Transactional
public class CurveStateServiceImpl extends ServiceImpl<CurveStateMapper, CurveState> implements ICurveStateService {


    @Autowired
    private CurveStateMapper curveStateMapper;

    @Override
    public IPage<CurveState> selectPage(Page<CurveState> page, CurveState curveState) {
        QueryWrapper<CurveState> query = Wrappers.query();
        QueryWrapper<CurveState> entityWrapper = getEntityWrapper(query, curveState);
        return curveStateMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<CurveState> selectList(CurveState curveState) {
        QueryWrapper<CurveState> query = Wrappers.query();
        QueryWrapper<CurveState> entityWrapper = getEntityWrapper(query, curveState);
        return curveStateMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<CurveState> getEntityWrapper(QueryWrapper<CurveState> entityWrapper, CurveState curveState) {
        //条件拼接
        if(curveState==null){
            return entityWrapper;
        }
        if (StringUtils.isNotBlank(curveState.getWorkShopId())) {
            entityWrapper.like(CurveState.WORK_SHOP_ID, curveState.getWorkShopId());
        }
        if (StringUtils.isNotBlank(curveState.getSystemCurveState())) {
            entityWrapper.like(CurveState.SYSTEM_CURVE_STATE, curveState.getSystemCurveState());
        }

        return entityWrapper;
    }

    @Override
    public List<CurveState> selectListById() {
        QueryWrapper<CurveState> query = Wrappers.query();
        query.lambda().isNotNull(CurveState::getId);
        return curveStateMapper.selectList(query);
    }

    @Override
    public Map<String, CurveState> curveStateMap() {
        QueryWrapper<CurveState> query = Wrappers.query();
        List<CurveState> curveStates = curveStateMapper.selectList(query);
        Map<String, CurveState> curveStateMap = curveStates.stream().collect(Collectors.toMap(CurveState::getWorkShopId, Function.identity()));
        return curveStateMap;
    }
}
